<?php

namespace plugin\ycMassage\app\api\controller;

use app\common\service\UploadService;
use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageUserAgentBill;
use plugin\ycMassage\app\model\MassageUserBill;
use plugin\ycMassage\app\model\MassageUserChannelsBill;
use plugin\ycMassage\app\model\MassageUserDealerBill;
use plugin\ycMassage\app\model\MassageUserFinanceAccount;
use plugin\ycMassage\app\model\MassageUserFreezeMoney;
use plugin\ycMassage\app\model\MassageUserSalesmanBill;
use plugin\ycMassage\app\model\MassageUserTechnician;
use plugin\ycMassage\app\model\MassageUserTechnicianBill;
use plugin\ycMassage\app\model\MassageUserWallet;
use plugin\ycMassage\app\model\MassageWithdrawal;
use plugin\ycMassage\utils\enum\ActionIdentity;
use plugin\ycMassage\utils\enum\Examine;
use plugin\ycMassage\utils\enum\NoticeType;
use plugin\ycMassage\utils\enum\State;
use plugin\ycMassage\utils\notice\Notice;
use support\Request;
use think\Exception;
use think\facade\Db;

class WalletController extends BaseController
{
    public function log(Request $request)
    {
        $limit = $request->get('limit', 20);
        $page = $request->get('page');
        $action = $request->get('action');
        $uid = $request->uid;
        $saas_appid = $request->saas_appid;
        $where = ['saas_appid' => $saas_appid, 'uid' => $uid];
        if ($action) {
            $where['action'] = $action;
        }
        $list = MassageUserBill::where($where)
            ->order('id desc')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ]);
        if ($list->isEmpty()) {
            return $this->fail('暂无记录');
        }
        return $this->successRes($list);
    }
    public function technicianLog(Request $request)
    {
        $action = $request->get('action');
        if ($action === 'freezeMoney') {
            return $this->freezeMoney($request, ActionIdentity::TECHNICIAN['value']);
        }
        $limit = $request->get('limit', 20);
        $page = $request->get('page');
        $uid = $request->uid;
        $saas_appid = $request->saas_appid;
        $where = ['saas_appid' => $saas_appid, 'uid' => $uid];
        if ($action) {
            $where['action'] = $action;
        }
        $list = MassageUserTechnicianBill::where($where)
            ->order('id desc')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ]);
        if ($list->isEmpty()) {
            return $this->fail('暂无记录');
        }
        return $this->successRes($list);
    }
    public function freezeMoney(Request $request, $action)
    {
        $limit = $request->get('limit', 20);
        $page = $request->get('page');
        $uid = $request->uid;
        $saas_appid = $request->saas_appid;
        $where = ['saas_appid' => $saas_appid, 'uid' => $uid, 'state' => State::NO['value'], 'action' => $action];
        $list = MassageUserFreezeMoney::where($where)
            ->order('id desc')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ])->each(function ($item) {
                $item->remarks = '解冻时间：' . $item->unfreezing_time;
            });
        if ($list->isEmpty()) {
            return $this->fail('暂无记录');
        }
        return $this->successRes($list);
    }
    public function agentLog(Request $request)
    {
        $action = $request->get('action');
        if ($action === 'freezeMoney') {
            return $this->freezeMoney($request, ActionIdentity::AGENT['value']);
        }
        $limit = $request->get('limit', 20);
        $page = $request->get('page');
        $uid = $request->uid;
        $saas_appid = $request->saas_appid;
        $where = ['saas_appid' => $saas_appid, 'uid' => $uid];
        if ($action) {
            $where['action'] = $action;
        }
        $list = MassageUserAgentBill::where($where)
            ->order('id desc')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ]);
        if ($list->isEmpty()) {
            return $this->fail('暂无记录');
        }
        return $this->successRes($list);
    }
    public function channelsLog(Request $request)
    {
        $action = $request->get('action');
        if ($action === 'freezeMoney') {
            return $this->freezeMoney($request, ActionIdentity::CHANNELS['value']);
        }
        $limit = $request->get('limit', 20);
        $page = $request->get('page');
        $uid = $request->uid;
        $saas_appid = $request->saas_appid;
        $where = ['saas_appid' => $saas_appid, 'uid' => $uid];
        if ($action) {
            $where['action'] = $action;
        }
        $list = MassageUserChannelsBill::where($where)
            ->order('id desc')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ]);
        if ($list->isEmpty()) {
            return $this->fail('暂无记录');
        }
        return $this->successRes($list);
    }
    public function dealerLog(Request $request)
    {
        $action = $request->get('action');
        if ($action === 'freezeMoney') {
            return $this->freezeMoney($request, ActionIdentity::DEALER['value']);
        }
        $limit = $request->get('limit', 20);
        $page = $request->get('page');
        $uid = $request->uid;
        $saas_appid = $request->saas_appid;
        $where = ['saas_appid' => $saas_appid, 'uid' => $uid];
        if ($action) {
            $where['action'] = $action;
        }
        $list = MassageUserDealerBill::where($where)
            ->order('id desc')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ]);
        if ($list->isEmpty()) {
            return $this->fail('暂无记录');
        }
        return $this->successRes($list);
    }
    public function salesmanLog(Request $request)
    {
        $action = $request->get('action');
        if ($action === 'freezeMoney') {
            return $this->freezeMoney($request, ActionIdentity::SALESMAN['value']);
        }
        $limit = $request->get('limit', 20);
        $page = $request->get('page');
        $uid = $request->uid;
        $saas_appid = $request->saas_appid;
        $where = ['saas_appid' => $saas_appid, 'uid' => $uid];
        if ($action) {
            $where['action'] = $action;
        }
        $list = MassageUserSalesmanBill::where($where)
            ->order('id desc')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ]);
        if ($list->isEmpty()) {
            return $this->fail('暂无记录');
        }
        return $this->successRes($list);
    }
    public function withdrawal(Request $request)
    {
        $action = $request->post('action');
        $channels = $request->post('channels');
        if (!$channels) {
            return $this->fail('请选择提现方式');
        }
        $money = $request->post('money', 0);
        if (!$money) {
            return $this->fail('请输入提现金额');
        }
        switch ($action) {
            case ActionIdentity::TECHNICIAN['value']:
                return $this->technicianWithdrawal($request);
            case ActionIdentity::AGENT['value']:
                return $this->agentWithdrawal($request);
            case ActionIdentity::CHANNELS['value']:
                return $this->channelsWithdrawal($request);
            case ActionIdentity::DEALER['value']:
                return $this->dealerWithdrawal($request);
            case ActionIdentity::SALESMAN['value']:
                return $this->salesmanWithdrawal($request);
            default:
                return $this->fail('暂不支持该身份提现');
        }
    }
    public function technicianWithdrawal(Request $request)
    {
        $uid = $request->uid;
        $saas_appid = $request->saas_appid;
        $channels = $request->post('channels');
        $money = (float)$request->post('money', 0);
        $action = ActionIdentity::TECHNICIAN['value'];
        $withdrawal = getHpConfig('', $saas_appid, 'withdrawal');
        $technician_withdrawal_fee = 0;
        $technician_min_withdrawal_money = 0;
        if (isset($withdrawal['technician_withdrawal_fee'])) {
            $technician_withdrawal_fee = (int)$withdrawal['technician_withdrawal_fee'];
        }
        if (isset($withdrawal['technician_min_withdrawal_money'])) {
            $technician_min_withdrawal_money = (float)$withdrawal['technician_min_withdrawal_money'];
        }
        if ($technician_min_withdrawal_money > 0 && $money < $technician_min_withdrawal_money) {
            return $this->fail('提现金额不能小于' . $technician_min_withdrawal_money);
        }
        Db::startTrans();
        try {
            $MassageUserFinanceAccount = MassageUserFinanceAccount::where(['uid' => $uid, 'channels' => $channels])->find();
            if (!$MassageUserFinanceAccount) {
                throw new Exception('请先绑定提现账号');
            }
            $price_fee = 0;
            if ($technician_withdrawal_fee > 0) {
                $price_fee = $money * $technician_withdrawal_fee / 100;
            }
            $price = $money - $price_fee;
            $MassageUserWallet = MassageUserWallet::where(['uid' => $uid])->find();
            if (!$MassageUserWallet) {
                throw new Exception('用户未开通钱包');
            }
            if($MassageUserWallet->technician_money>$money + $price_fee){
                $price = $money + $price_fee;
            }else{
                if($MassageUserWallet->technician_money>$money){
                    $price=$MassageUserWallet->technician_money;
                    $money=$price-$price_fee;
                }else{
                    $price=$money;
                    $money=$money-$price_fee;
                }
            }
            $MassageWithdrawal = new MassageWithdrawal();
            $MassageWithdrawal->uid = $uid;
            $MassageWithdrawal->saas_appid = $saas_appid;
            $MassageWithdrawal->channels = $channels;
            $MassageWithdrawal->name = $MassageUserFinanceAccount->name;
            $MassageWithdrawal->mobile = $MassageUserFinanceAccount->mobile;
            $MassageWithdrawal->openid = $MassageUserFinanceAccount->openid;
            $MassageWithdrawal->mp_openid = $MassageUserFinanceAccount->mp_openid;
            $MassageWithdrawal->account = $MassageUserFinanceAccount->account;
            $MassageWithdrawal->bank = $MassageUserFinanceAccount->bank;
            if ($MassageUserFinanceAccount->qrcode) {
                $MassageWithdrawal->qrcode = UploadService::path($MassageUserFinanceAccount->qrcode);
            }
            $MassageWithdrawal->money = $money;
            $MassageWithdrawal->old_withdrawal_price = $MassageUserWallet->technician_money;
            $MassageWithdrawal->new_withdrawal_price = $MassageUserWallet->technician_money - $price;
            $MassageWithdrawal->state = Examine::WAIT['value'];
            $MassageWithdrawal->price_fee = $price_fee;
            $MassageWithdrawal->withdrawal_fee = $technician_withdrawal_fee;
            $MassageWithdrawal->action = $action;
            $MassageWithdrawal->save();
            MassageUserTechnicianBill::decrease('提现', $uid, $price);
            Db::commit();
        } catch (\Throwable $th) {
            Db::rollback();
            return $this->fail($th->getMessage());
        }
        $model = MassageUserTechnician::where(['uid' => $uid])->find();
        $notice = new Notice();
        $notice->saas_appid = $saas_appid;
        $notice->role = ['ADMIN'];
        $notice->type = NoticeType::WORK_ORDER['value'];
        $notice->setData([
            'service_name' => '技师提现审核',
            'name' => $model->name . '(' . $model->uid . ')，提现金额：' . $money . '元',
            'time' => date('Y-m-d H:i:s'),
        ]);
        $notice->send();
        return $this->success('提现申请已提交，请等待审核');
    }
    public function agentWithdrawal(Request $request)
    {
        $uid = $request->uid;
        $saas_appid = $request->saas_appid;
        $channels = $request->post('channels');
        $money = (float)$request->post('money', 0);
        $action = ActionIdentity::AGENT['value'];
        $withdrawal = getHpConfig('', $saas_appid, 'withdrawal');
        $agent_withdrawal_fee = 0;
        $agent_min_withdrawal_money = 0;
        if (isset($withdrawal['agent_withdrawal_fee'])) {
            $agent_withdrawal_fee = (int)$withdrawal['agent_withdrawal_fee'];
        }
        if (isset($withdrawal['agent_min_withdrawal_money'])) {
            $agent_min_withdrawal_money = (float)$withdrawal['agent_min_withdrawal_money'];
        }
        if ($agent_min_withdrawal_money > 0 && $money < $agent_min_withdrawal_money) {
            return $this->fail('提现金额不能小于' . $agent_min_withdrawal_money);
        }
        Db::startTrans();
        try {
            $MassageUserFinanceAccount = MassageUserFinanceAccount::where(['uid' => $uid, 'channels' => $channels])->find();
            if (!$MassageUserFinanceAccount) {
                throw new Exception('请先绑定提现账号');
            }
            $price_fee = 0;
            if ($agent_withdrawal_fee > 0) {
                $price_fee = $money * $agent_withdrawal_fee / 100;
            }
            $price = $money - $price_fee;
            $MassageUserWallet = MassageUserWallet::where(['uid' => $uid])->find();
            if (!$MassageUserWallet) {
                throw new Exception('用户未开通钱包');
            }
            if($MassageUserWallet->agent_money>$money + $price_fee){
                $price = $money + $price_fee;
            }else{
                if($MassageUserWallet->agent_money>$money){
                    $price=$MassageUserWallet->agent_money;
                    $money=$price-$price_fee;
                }else{
                    $price=$money;
                    $money=$money-$price_fee;
                }
            }
            $MassageWithdrawal = new MassageWithdrawal();
            $MassageWithdrawal->uid = $uid;
            $MassageWithdrawal->saas_appid = $saas_appid;
            $MassageWithdrawal->channels = $channels;
            $MassageWithdrawal->name = $MassageUserFinanceAccount->name;
            $MassageWithdrawal->mobile = $MassageUserFinanceAccount->mobile;
            $MassageWithdrawal->openid = $MassageUserFinanceAccount->openid;
            $MassageWithdrawal->mp_openid = $MassageUserFinanceAccount->mp_openid;
            $MassageWithdrawal->account = $MassageUserFinanceAccount->account;
            $MassageWithdrawal->bank = $MassageUserFinanceAccount->bank;
            if ($MassageUserFinanceAccount->qrcode) {
                $MassageWithdrawal->qrcode = UploadService::path($MassageUserFinanceAccount->qrcode);
            }
            $MassageWithdrawal->money = $money;
            $MassageWithdrawal->old_withdrawal_price = $MassageUserWallet->agent_money;
            $MassageWithdrawal->new_withdrawal_price = $MassageUserWallet->agent_money - $price;
            $MassageWithdrawal->state = Examine::WAIT['value'];
            $MassageWithdrawal->price_fee = $price_fee;
            $MassageWithdrawal->withdrawal_fee = $agent_withdrawal_fee;
            $MassageWithdrawal->action = $action;
            $MassageWithdrawal->save();
            MassageUserAgentBill::decrease('提现', $uid, $price);
            Db::commit();
        } catch (\Throwable $th) {
            Db::rollback();
            return $this->fail($th->getMessage());
        }
        $notice = new Notice();
        $notice->saas_appid = $saas_appid;
        $notice->role = ['ADMIN'];
        $notice->type = NoticeType::WORK_ORDER['value'];
        $notice->setData([
            'service_name' => '代理提现审核',
            'name' => 'UID:' . $uid . '，提现金额：' . $money . '元',
            'time' => date('Y-m-d H:i:s'),
        ]);
        $notice->send();
        return $this->success('提现申请已提交，请等待审核');
    }
    public function channelsWithdrawal(Request $request){
        $uid = $request->uid;
        $saas_appid = $request->saas_appid;
        $channels = $request->post('channels');
        $money = (float)$request->post('money', 0);
        $action = ActionIdentity::CHANNELS['value'];
        $withdrawal = getHpConfig('', $saas_appid, 'withdrawal');
        $channels_withdrawal_fee = 0;
        $channels_min_withdrawal_money = 0;
        if (isset($withdrawal['channels_withdrawal_fee'])) {
            $channels_withdrawal_fee = (int)$withdrawal['channels_withdrawal_fee'];
        }
        if (isset($withdrawal['channels_min_withdrawal_money'])) {
            $channels_min_withdrawal_money = (float)$withdrawal['channels_min_withdrawal_money'];
        }
        if ($channels_min_withdrawal_money > 0 && $money < $channels_min_withdrawal_money) {
            return $this->fail('提现金额不能小于' . $channels_min_withdrawal_money);
        }
        Db::startTrans();
        try {
            $MassageUserFinanceAccount = MassageUserFinanceAccount::where(['uid' => $uid, 'channels' => $channels])->find();
            if (!$MassageUserFinanceAccount) {
                throw new Exception('请先绑定提现账号');
            }
            $price_fee = 0;
            if ($channels_withdrawal_fee > 0) {
                $price_fee = $money * $channels_withdrawal_fee / 100;
            }
            $price = $money - $price_fee;
            $MassageUserWallet = MassageUserWallet::where(['uid' => $uid])->find();
            if (!$MassageUserWallet) {
                throw new Exception('用户未开通钱包');
            }
            if($MassageUserWallet->channels_money>$money + $price_fee){
                $price = $money + $price_fee;
            }else{
                if($MassageUserWallet->channels_money>$money){
                    $price=$MassageUserWallet->channels_money;
                    $money=$price-$price_fee;
                }else{
                    $price=$money;
                    $money=$money-$price_fee;
                }
            }
            $MassageWithdrawal = new MassageWithdrawal();
            $MassageWithdrawal->uid = $uid;
            $MassageWithdrawal->saas_appid = $saas_appid;
            $MassageWithdrawal->channels = $channels;
            $MassageWithdrawal->name = $MassageUserFinanceAccount->name;
            $MassageWithdrawal->mobile = $MassageUserFinanceAccount->mobile;
            $MassageWithdrawal->openid = $MassageUserFinanceAccount->openid;
            $MassageWithdrawal->mp_openid = $MassageUserFinanceAccount->mp_openid;
            $MassageWithdrawal->account = $MassageUserFinanceAccount->account;
            $MassageWithdrawal->bank = $MassageUserFinanceAccount->bank;
            if ($MassageUserFinanceAccount->qrcode) {
                $MassageWithdrawal->qrcode = UploadService::path($MassageUserFinanceAccount->qrcode);
            }
            $MassageWithdrawal->money = $money;
            $MassageWithdrawal->old_withdrawal_price = $MassageUserWallet->channels_money;
            $MassageWithdrawal->new_withdrawal_price = $MassageUserWallet->channels_money - $price;
            $MassageWithdrawal->state = Examine::WAIT['value'];
            $MassageWithdrawal->price_fee = $price_fee;
            $MassageWithdrawal->withdrawal_fee = $channels_withdrawal_fee;
            $MassageWithdrawal->action = $action;
            $MassageWithdrawal->save();
            MassageUserChannelsBill::decrease('提现', $uid, $price);
            Db::commit();
        } catch (\Throwable $th) {
            Db::rollback();
            return $this->fail($th->getMessage());
        }
        $notice = new Notice();
        $notice->saas_appid = $saas_appid;
        $notice->role = ['ADMIN'];
        $notice->type = NoticeType::WORK_ORDER['value'];
        $notice->setData([
            'service_name' => '渠道商提现审核',
            'name' => 'UID:' . $uid . '，提现金额：' . $money . '元',
            'time' => date('Y-m-d H:i:s'),
        ]);
        $notice->send();
        return $this->success('提现申请已提交，请等待审核');
    }
    public function dealerWithdrawal(Request $request){
        $uid = $request->uid;
        $saas_appid = $request->saas_appid;
        $channels = $request->post('channels');
        $money = (float)$request->post('money', 0);
        $action = ActionIdentity::DEALER['value'];
        $withdrawal = getHpConfig('', $saas_appid, 'withdrawal');
        $dealer_withdrawal_fee = 0;
        $dealer_min_withdrawal_money = 0;
        if (isset($withdrawal['dealer_withdrawal_fee'])) {
            $dealer_withdrawal_fee = (int)$withdrawal['dealer_withdrawal_fee'];
        }
        if (isset($withdrawal['dealer_min_withdrawal_money'])) {
            $dealer_min_withdrawal_money = (float)$withdrawal['dealer_min_withdrawal_money'];
        }
        if ($dealer_min_withdrawal_money > 0 && $money < $dealer_min_withdrawal_money) {
            return $this->fail('提现金额不能小于' . $dealer_min_withdrawal_money);
        }
        Db::startTrans();
        try {
            $MassageUserFinanceAccount = MassageUserFinanceAccount::where(['uid' => $uid, 'channels' => $channels])->find();
            if (!$MassageUserFinanceAccount) {
                throw new Exception('请先绑定提现账号');
            }
            $price_fee = 0;
            if ($dealer_withdrawal_fee > 0) {
                $price_fee = $money * $dealer_withdrawal_fee / 100;
            }
            $MassageUserWallet = MassageUserWallet::where(['uid' => $uid])->find();
            if (!$MassageUserWallet) {
                throw new Exception('用户未开通钱包');
            }
            if($MassageUserWallet->dealer_money>$money + $price_fee){
                $price = $money + $price_fee;
            }else{
                if($MassageUserWallet->dealer_money>$money){
                    $price=$MassageUserWallet->dealer_money;
                    $money=$price-$price_fee;
                }else{
                    $price=$money;
                    $money=$money-$price_fee;
                }
            }
            $MassageWithdrawal = new MassageWithdrawal();
            $MassageWithdrawal->uid = $uid;
            $MassageWithdrawal->saas_appid = $saas_appid;
            $MassageWithdrawal->channels = $channels;
            $MassageWithdrawal->name = $MassageUserFinanceAccount->name;
            $MassageWithdrawal->mobile = $MassageUserFinanceAccount->mobile;
            $MassageWithdrawal->openid = $MassageUserFinanceAccount->openid;
            $MassageWithdrawal->mp_openid = $MassageUserFinanceAccount->mp_openid;
            $MassageWithdrawal->account = $MassageUserFinanceAccount->account;
            $MassageWithdrawal->bank = $MassageUserFinanceAccount->bank;
            if ($MassageUserFinanceAccount->qrcode) {
                $MassageWithdrawal->qrcode = UploadService::path($MassageUserFinanceAccount->qrcode);
            }
            $MassageWithdrawal->money = $money;
            $MassageWithdrawal->old_withdrawal_price = $MassageUserWallet->dealer_money;
            $MassageWithdrawal->new_withdrawal_price = $MassageUserWallet->dealer_money - $price;
            $MassageWithdrawal->state = Examine::WAIT['value'];
            $MassageWithdrawal->price_fee = $price_fee;
            $MassageWithdrawal->withdrawal_fee = $dealer_withdrawal_fee;
            $MassageWithdrawal->action = $action;
            $MassageWithdrawal->save();
            MassageUserDealerBill::decrease('提现', $uid, $price);
            Db::commit();
        } catch (\Throwable $th) {
            Db::rollback();
            return parent::fail($th->getMessage());
        }
        $notice = new Notice();
        $notice->saas_appid = $saas_appid;
        $notice->role = ['ADMIN'];
        $notice->type = NoticeType::WORK_ORDER['value'];
        $notice->setData([
            'service_name' => '分销提现审核',
            'name' => 'UID:' . $uid . '，提现金额：' . $money . '元',
            'time' => date('Y-m-d H:i:s'),
        ]);
        $notice->send();
        return $this->success('提现申请已提交，请等待审核');
    }
    public function salesmanWithdrawal(Request $request)
    {
        $uid = $request->uid;
        $saas_appid = $request->saas_appid;
        $channels = $request->post('channels');
        $money = (float)$request->post('money', 0);
        $action = ActionIdentity::SALESMAN['value'];
        $withdrawal = getHpConfig('', $saas_appid, 'withdrawal');
        $salesman_withdrawal_fee = 0;
        $salesman_min_withdrawal_money = 0;
        if (isset($withdrawal['salesman_withdrawal_fee'])) {
            $salesman_withdrawal_fee = (int)$withdrawal['salesman_withdrawal_fee'];
        }
        if (isset($withdrawal['salesman_min_withdrawal_money'])) {
            $salesman_min_withdrawal_money = (float)$withdrawal['salesman_min_withdrawal_money'];
        }
        if ($salesman_min_withdrawal_money > 0 && $money < $salesman_min_withdrawal_money) {
            return $this->fail('提现金额不能小于' . $salesman_min_withdrawal_money);
        }
        Db::startTrans();
        try {
            $MassageUserFinanceAccount = MassageUserFinanceAccount::where(['uid' => $uid, 'channels' => $channels])->find();
            if (!$MassageUserFinanceAccount) {
                throw new Exception('请先绑定提现账号');
            }
            $price_fee = 0;
            if ($salesman_withdrawal_fee > 0) {
                $price_fee = $money * $salesman_withdrawal_fee / 100;
            }
            $MassageUserWallet = MassageUserWallet::where(['uid' => $uid])->find();
            if (!$MassageUserWallet) {
                throw new Exception('用户未开通钱包');
            }
            if($MassageUserWallet->salesman_money>$money + $price_fee){
                $price = $money + $price_fee;
            }else{
                if($MassageUserWallet->salesman_money>$money){
                    $price=$MassageUserWallet->salesman_money;
                    $money=$price-$price_fee;
                }else{
                    $price=$money;
                    $money=$money-$price_fee;
                }
            }
            $MassageWithdrawal = new MassageWithdrawal();
            $MassageWithdrawal->uid = $uid;
            $MassageWithdrawal->saas_appid = $saas_appid;
            $MassageWithdrawal->channels = $channels;
            $MassageWithdrawal->name = $MassageUserFinanceAccount->name;
            $MassageWithdrawal->mobile = $MassageUserFinanceAccount->mobile;
            $MassageWithdrawal->openid = $MassageUserFinanceAccount->openid;
            $MassageWithdrawal->mp_openid = $MassageUserFinanceAccount->mp_openid;
            $MassageWithdrawal->account = $MassageUserFinanceAccount->account;
            $MassageWithdrawal->bank = $MassageUserFinanceAccount->bank;
            if ($MassageUserFinanceAccount->qrcode) {
                $MassageWithdrawal->qrcode = UploadService::path($MassageUserFinanceAccount->qrcode);
            }
            $MassageWithdrawal->money = $money;
            $MassageWithdrawal->old_withdrawal_price = $MassageUserWallet->salesman_money;
            $MassageWithdrawal->new_withdrawal_price = $MassageUserWallet->salesman_money - $price;
            $MassageWithdrawal->state = Examine::WAIT['value'];
            $MassageWithdrawal->price_fee = $price_fee;
            $MassageWithdrawal->withdrawal_fee = $salesman_withdrawal_fee;
            $MassageWithdrawal->action = $action;
            $MassageWithdrawal->save();
            MassageUserSalesmanBill::decrease('提现', $uid, $price);
            Db::commit();
        } catch (\Throwable $th) {
            Db::rollback();
            return $this->fail($th->getMessage());
        }
        $notice = new Notice();
        $notice->saas_appid = $saas_appid;
        $notice->role = ['ADMIN'];
        $notice->type = NoticeType::WORK_ORDER['value'];
        $notice->setData([
            'service_name' => '业务员提现审核',
            'name' => 'UID:' . $uid . '，提现金额：' . $money . '元',
            'time' => date('Y-m-d H:i:s'),
        ]);
        $notice->send();
        return $this->success('提现申请已提交，请等待审核');
    }
}
